package com.mobile.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.mobile.business.grid.IBaseBusiness;
import com.mobile.dao.IBaseIbatisDAO;
import com.mobile.dao.IBaseSpringDAO;
import com.mobile.gridUtil.FilterBean;
import com.mobile.gridUtil.GridFilter;
import com.mobile.gridUtil.ParseFilterData;
import com.mobile.model.SysMenu;
import com.mobile.struts.action.tools.AssociateTableInfo;
import com.mobile.tools.BeanUtil;
import com.mobile.tools.PageBean;

/**
 * 基础业务类，每个具体业务类不需继承此类
 * 
 * @author hf
 * 
 */
public class BaseBusiness implements IBaseBusiness {
	

	private final Logger LOGGER = LogManager
			.getLogger(BaseBusiness.class);

	public IBaseSpringDAO baseDAO;
	
	public IBaseIbatisDAO baseIbatisDAO;
	

	public void setBaseIbatisDAO(IBaseIbatisDAO baseIbatisDAO) {
		this.baseIbatisDAO = baseIbatisDAO;
	}
	
	public Object queryForObject(String statementName){
		return this.baseIbatisDAO.queryForObject(statementName);
	}
	
	public Object queryForObject(String statementName,Object parameterObject){
		return this.baseIbatisDAO.queryForObject(statementName,parameterObject);
	}
	
	public Object queryForList(String statementName){
		return this.baseIbatisDAO.queryForList(statementName);
	}
	
	public IBaseSpringDAO getbaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseSpringDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public void deleteById(Class clazz, String uniqueCode) {
		// TODO Auto-generated method stub
		this.baseDAO.deleteById(clazz, uniqueCode);
	}

	public void deleteById(Class clazz, String[] uniqueCodes) {
		// TODO Auto-generated method stub
		this.baseDAO.deleteById(clazz, uniqueCodes);
	}

	public void mergeCollection(Collection entities) {
		// TODO Auto-generated method stub
		Iterator it=entities.iterator();
		while(it.hasNext()){
			Object obj=it.next();
			this.baseDAO.merge(obj);
		}
	}

	public void mergeCollection(String entityName, Collection entities) {
		// TODO Auto-generated method stub
		Iterator it=entities.iterator();
		while(it.hasNext()){
			Object obj=it.next();
			this.baseDAO.merge(entityName,obj);
		}
	}

	public void saveCollection(Collection entities) {
		// TODO Auto-generated method stub
		Iterator it=entities.iterator();
		while(it.hasNext()){
			Object obj=it.next();
			this.baseDAO.save(obj);
		}
	}

	public void saveCollection(String entityName, Collection entities) {
		// TODO Auto-generated method stub
		Iterator it=entities.iterator();
		while(it.hasNext()){
			Object obj=it.next();
			this.baseDAO.save(entityName,obj);
		}
	}
	
	public void delete(Object entity, LockMode lockMode) {
		// TODO Auto-generated method stub
		baseDAO.delete(entity, lockMode);
	}

	public void delete(Object entity) {
		// TODO Auto-generated method stub
		baseDAO.delete(entity);
	}

	public void delete(String entityName, Object entity, LockMode lockMode) {
		// TODO Auto-generated method stub
		baseDAO.delete(entityName, entity, lockMode);
	}

	public void delete(String entityName, Object entity) {
		// TODO Auto-generated method stub
		baseDAO.delete(entityName, entity);
	}

	public void deleteAll(Collection entities) {
		// TODO Auto-generated method stub
		baseDAO.delete(entities);
	}

	public Object merge(Object entity) {
		// TODO Auto-generated method stub
		return baseDAO.merge(entity);
	}

	public Object merge(String entityName, Object entity) {
		// TODO Auto-generated method stub
		return baseDAO.merge(entityName, entity);
	}

	public String save(Object o) {
		// TODO Auto-generated method stub
		return (String) baseDAO.save(o);
	}

	public String save(String entityName, Object entity) {
		// TODO Auto-generated method stub
		return (String) baseDAO.save(entityName, entity);
	}

	public void saveOrUpdate(Object entity) {
		// TODO Auto-generated method stub
		baseDAO.saveOrUpdate(entity);
	}

	public void saveOrUpdate(String entityName, Object entity) {
		// TODO Auto-generated method stub
		baseDAO.saveOrUpdate(entityName, entity);
	}

	public void saveOrUpdateAll(Collection entities) {
		// TODO Auto-generated method stub
		baseDAO.saveOrUpdateAll(entities);
	}

	public void update(Object entity, LockMode lockMode) {
		// TODO Auto-generated method stub
		baseDAO.update(entity, lockMode);
	}

	public void update(Object entity) {
		// TODO Auto-generated method stub
		baseDAO.update(entity);
	}

	public void update(String entityName, Object entity, LockMode lockMode) {
		// TODO Auto-generated method stub
		baseDAO.update(entityName, entity, lockMode);
	}

	public void update(String entityName, Object entity) {
		// TODO Auto-generated method stub
		baseDAO.update(entityName, entity);
	}

	public List getAllList(Object pojo) {
		return baseDAO.getAllList(pojo);
	}
	public List findByExample(Object o){
		return baseDAO.findByExample(o);
	}
	public List findByExample(Object o,String[] orders){
		return baseDAO.findByExample(o, orders);
	}
	public List findByExample(Object o,List<Order> orders){
		return baseDAO.findByExample(o, orders);
	}
	public Object findById(Class clazz,String id){
		return baseDAO.findById(clazz, id);
	}
	
	public List findByCriteria(DetachedCriteria criteria){
		return baseDAO.findByCriteria(criteria);
	}
	public List findByCriteria(DetachedCriteria criteria,
            int firstResult,
            int maxResults){
		return baseDAO.findByCriteria(criteria, firstResult, maxResults);
	}
	public Object get(Class entityClass, Serializable id, LockMode lockMode) {
		// TODO Auto-generated method stub
		return baseDAO.get(entityClass, id, lockMode);
	}

	public Object get(Class entityClass, Serializable id) {
		// TODO Auto-generated method stub
		return baseDAO.get(entityClass, id);
	}

	public Object get(String entityName, Serializable id, LockMode lockMode) {
		// TODO Auto-generated method stub
		return baseDAO.get(entityName, id, lockMode);
	}

	public Object get(String entityName, Serializable id) {
		// TODO Auto-generated method stub
		return baseDAO.get(entityName, id);
	}

	public Object load(Class entityClass, Serializable id, LockMode lockMode) {
		// TODO Auto-generated method stub
		return baseDAO.load(entityClass, id, lockMode);
	}

	public Object load(Class entityClass, Serializable id) {
		// TODO Auto-generated method stub
		return baseDAO.load(entityClass, id);
	}

	public void load(Object entity, Serializable id) {
		// TODO Auto-generated method stub
		baseDAO.load(entity, id);
	}

	public Object load(String entityName, Serializable id, LockMode lockMode) {
		// TODO Auto-generated method stub
		return baseDAO.load(entityName, id, lockMode);
	}

	public Object load(String entityName, Serializable id) {
		// TODO Auto-generated method stub
		return baseDAO.load(entityName, id);
	}

	public Object mergeNotNull(String uniqueCode,Object entity) {
		// TODO Auto-generated method stub
		SysMenu source = (SysMenu)this.get(entity.getClass(), uniqueCode);			
		return BeanUtil
						.addNewProperties(source, entity);
	}
	
	/**
	 * 分页
	 * @param start 			跳转的页数（开始的页数）
	 * @param pageSize			一页有多少行
	 * @param pojo				用来创建org.hibernate.criterion.Example的pojo类
	 * @param gridFilterList	查询条件树组
	 * @return
	 */
	public PageBean getPageRecord(int start,int pageSize,Object pojo,List<GridFilter> gridFilterList){
		/**
		 * list 			从查询中获得的数据集合
		 * startid			设置开始查询的位置
		 * total			总共返回的数据的总数
		 */
		PageBean pageBean=new PageBean();
		List list=new ArrayList();
		int startid=0;
		int total=0;
		pageBean.setPageSize(pageSize);
		/**
		 * 查询总数，设置总条数
		 */
		total=baseDAO.getTotalSize(pojo, gridFilterList);
		pageBean.setTotalSize(total);
		
		/**
		 * 设置总共页数
		 */
		if(total>pageSize)
		{
			if(total%pageSize==0)
			{
				pageBean.setTotalPage((int)((double)total/(double)pageSize));
			}
			else
			{
				pageBean.setTotalPage((int)(1.0d+(double)total/(double)pageSize));
			}
		}
		else
		{
			total=1;
			pageBean.setTotalPage(1);
		}
		
		/**
		 * 判断当前页和确定初始页
		 */
		if(start<=0)
		{
			startid=0;
		}
		else
		{
			if(startid>pageBean.getTotalPage())
			{
				startid=pageBean.getTotalPage();
				startid=pageSize*(startid-1);
			}
			else
			{
				startid=pageSize*(start-1);
			}
		}
		pageBean.setCurrentPate(start);
		list=baseDAO.getPageList(startid, pageSize, pojo, gridFilterList);
		pageBean.setList(list);
		return pageBean;
	}
	/**
	 * 分页
	 * @param start 			跳转的页数（开始的页数）
	 * @param pageSize			一页有多少行
	 * @param total				符合条件的总条数
	 * @param list				查询出来的数据
	 * @param gridFilterList	查询条件树组
	 * @return
	 */
	public PageBean getPageRecord(int start,int pageSize,Object pojo,List<GridFilter> gridFilterList,List<AssociateTableInfo> associateList){
		/**
		 * list 			从查询中获得的数据集合
		 * startid			设置开始查询的位置
		 * total			总共返回的数据的总数
		 */
		PageBean pageBean=new PageBean();
		List list=new ArrayList();
		int startid=0;
		int total=0;
		pageBean.setPageSize(pageSize);
		/**
		 * 查询总数，设置总条数
		 */
		total=baseDAO.getTotalSize(pojo, gridFilterList,associateList);
		pageBean.setTotalSize(total);
		
		/**
		 * 设置总共页数
		 */
		if(total>pageSize)
		{
			if(total%pageSize==0)
			{
				pageBean.setTotalPage((int)((double)total/(double)pageSize));
			}
			else
			{
				pageBean.setTotalPage((int)(1.0d+(double)total/(double)pageSize));
			}
		}
		else
		{
			total=1;
			pageBean.setTotalPage(1);
		}
		
		/**
		 * 判断当前页和确定初始页
		 */
		if(start<=0)
		{
			startid=0;
		}
		else
		{
			if(startid>pageBean.getTotalPage())
			{
				startid=pageBean.getTotalPage();
				startid=pageSize*(startid-1);
			}
			else
			{
				startid=pageSize*(start-1);
			}
		}
		pageBean.setCurrentPate(start);
		list=baseDAO.getPageList(startid, pageSize, pojo, gridFilterList,associateList);
		pageBean.setList(list);
		return pageBean;
	}
	/**
	 * grid过滤
	 * @param start
	 * @param pageSize
	 * @param form
	 * @param map
	 * @return
	 */
	public PageBean getPageBean(int start,int pageSize,Object pojo,Map<String,FilterBean> map){
		List<GridFilter> gridFilterList=new ArrayList<GridFilter>();
		gridFilterList=ParseFilterData.parse(map);
		return getPageRecord(start, pageSize, pojo,gridFilterList);
	}
	/**
	 * grid过滤
	 * @param start
	 * @param pageSize
	 * @param form
	 * @param map
	 * @param list
	 * @return
	 */
	public PageBean getPageBean(int start, int pageSize, Object pojo,
			Map<String, FilterBean> map, List<AssociateTableInfo> associateList) {
		List<GridFilter> gridFilterList=new ArrayList<GridFilter>();
		gridFilterList=ParseFilterData.parse(map);
		return getPageRecord(start, pageSize, pojo,gridFilterList,associateList);
	}
	/**
	 * 获得所有数据
	 * @param cl				域模型的类
	 * @param gridFilterList	要过滤的列表值
	 * @return					列表
	 */
	public List getAllList(Class cl,List<GridFilter> gridFilterList){
		return baseDAO.getAllList(cl, gridFilterList);
	}
	/**
	 * 获得所有数据
	 * @param pojo				域模型的类
	 * @param gridFilterList	要过滤的列表值
	 * @return					列表
	 */
	public List getAllList(Object pojo,List<GridFilter> gridFilterList){
		return baseDAO.getAllList(pojo, gridFilterList);
	}
	/**
	 * 获得所有数据
	 * @param cl				域模型的类
	 * @param map				需要处理的的map数据
	 * @return					列表
	 */
	public List getAllList(Class cl,Map<String,FilterBean> map){
		List<GridFilter> gridFilterList=new ArrayList<GridFilter>();
		gridFilterList=ParseFilterData.parse(map);		
		return getAllList(cl,gridFilterList);
	}
	/**
	 * 获得所有数据
	 * @param map				需要处理的的map数据
	 * @return					列表
	 */
	public List getAllList(Object pojo,Map<String,FilterBean> map){
		List<GridFilter> gridFilterList=new ArrayList<GridFilter>();
		gridFilterList=ParseFilterData.parse(map);		
		return getAllList(pojo,gridFilterList);
	}
}
